

//
//  CXBNoInterNetView.m
//  车信帮3.0
//
//  Created by 刘耀宗 on 16/8/6.
//  Copyright © 2016年 lifuli. All rights reserved.
//

#import "CXBNoInterNetView.h"
@interface CXBNoInterNetView ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *reLoadBtn;
@property (nonatomic, strong) UILabel *label;

@end

@implementation CXBNoInterNetView
-(UILabel *)label
{
    if (_label == nil) {
        _label = [[UILabel alloc] init];
        _label.text=@"加载数据失败,请检测您的网络";
        _label.textColor=CXBColor(153, 153, 153);
        _label.font=CXBFont(16);
    }
    return _label;


}
-(UIImageView *)imageView
{
    if (_imageView == nil) {
        _imageView = [[UIImageView alloc] init];
        _imageView.image=[UIImage imageNamed:@"ic_loadfail"];
    }
    return _imageView;


}
+(instancetype)shareNoNetView
{
    static id instance;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        instance=[[self alloc] init];
        
    });
    return instance;
}

-(UIButton *)reLoadBtn
{
    if (_reLoadBtn == nil) {
        _reLoadBtn = [[UIButton alloc] init];
        [_reLoadBtn setTitle:@"刷新重试" forState:UIControlStateNormal];
        [_reLoadBtn setImage:[UIImage imageNamed:@"ic_loading"] forState:UIControlStateNormal];
        [_reLoadBtn setTitleColor:CXBColor(51, 51, 51) forState:UIControlStateNormal];
        [_reLoadBtn setBackgroundImage:btnHighBGImage forState:UIControlStateHighlighted];
        _reLoadBtn.layer.cornerRadius=22.5;
        _reLoadBtn.layer.masksToBounds=YES;
        _reLoadBtn.layer.borderColor=[CXBColor(51, 51, 51) CGColor];
        _reLoadBtn.layer.borderWidth=1;
        [_reLoadBtn setBackgroundColor:[UIColor whiteColor]];
         [_reLoadBtn addTarget:self action:@selector(clickForReLoad) forControlEvents:UIControlEventTouchUpInside];
        [_reLoadBtn sizeToFit];
    }
    return _reLoadBtn;
    
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self prepareUI];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
         [self prepareUI];
    }
    return self;
}
-(void)prepareUI
{
    self.backgroundColor=[UIColor whiteColor];
    NSArray *views=@[self.reLoadBtn,self.imageView,self.label];
    [self sd_addSubviews:views];
    self.imageView.sd_layout
    .centerXEqualToView(self)
    .widthIs(180)
    .heightIs(180)
    .topSpaceToView(self,64+70);
    self.label.sd_layout
    .centerXEqualToView(self)
    .topSpaceToView(self.imageView,10)
    .autoHeightRatio(0);
    [self.label setSingleLineAutoResizeWithMaxWidth:250];

    self.reLoadBtn.sd_layout
    .centerXEqualToView(self)
    .topSpaceToView(self.label,30)
    .widthIs(280)
    .heightIs(45);
    
    self.reLoadBtn.imageView.sd_layout
    .widthIs(25)
    .centerYEqualToView(self.reLoadBtn)
    .leftSpaceToView(self.reLoadBtn,50)
    .heightIs(25);
    
    // 设置button的label的约束
    self.reLoadBtn.titleLabel.sd_layout
    .centerYEqualToView(self.reLoadBtn)
    .leftSpaceToView(self.reLoadBtn.imageView,34)
    .widthIs(150);
 
  

}
-(void)clickForBack
{
    self.backBlock();
    
}
-(void)clickForReLoad
{  CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 0.5;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = MAXFLOAT;
    
    [self.reLoadBtn.imageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

    self.reloadBlock();
}
-(void)setFlag:(BOOL)flag
{
     _flag=flag;
    [self.reLoadBtn.imageView.layer removeAllAnimations];
  

}

@end
